/*
 * Copyright (C) 2012, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.benchmark;

/**
 *
 * @author Laurent WOUTERS
 */
public class RDFWriter implements edu.lehigh.swat.bench.uba.Writer {
    private xowl.interpreter.Repository repo;
    private xowl.lang.owl2.Ontology onto;
    private xowl.lang.owl2.IRI current;
    private xowl.interpreter.Changeset changeset;
    private int counter;
    
    public int getCounter() { return counter; }
    
    public RDFWriter(xowl.interpreter.Repository repo, xowl.lang.owl2.Ontology onto) {
        this.repo = repo;
        this.onto = onto;
    }
    
    @Override public void start() { }
    @Override public void end() { }
    @Override public void startFile(String fileName) { }
    @Override public void endFile() { }

    @Override public void startSection(int classType, String id) {
        if (current != null)
            java.lang.System.out.println("Oops!");
        xowl.lang.owl2.IRI cIRI = repo.getIRI("http://swat.cse.lehigh.edu/onto/univ-bench.owl#"+edu.lehigh.swat.bench.uba.Generator.CLASS_TOKEN[classType]);
        current = repo.getIRI(id);
        changeset = new xowl.interpreter.Changeset();
        xowl.lang.owl2.ClassAssertion axiom = new xowl.lang.owl2.ClassAssertion();
        axiom.setClasse(cIRI);
        axiom.setIndividual(current);
        changeset.add(new xowl.interpreter.Change(onto, axiom, true));
        counter++;
    }

    @Override public void startAboutSection(int classType, String id) {
        startSection(classType, id);
    }

    @Override public void endSection(int classType) {
        try { repo.apply(changeset); }
        catch (xowl.interpreter.XOWLException ex) { }
        changeset = null;
        current = null;
    }

    @Override public void addProperty(int property, String value, boolean isResource) {
        xowl.lang.owl2.IRI pIRI = repo.getIRI("http://swat.cse.lehigh.edu/onto/univ-bench.owl#"+edu.lehigh.swat.bench.uba.Generator.PROP_TOKEN[property]);
        if (!isResource) {
            xowl.lang.owl2.Literal lit = getLiteral(value);
            xowl.lang.owl2.DataPropertyAssertion axiom = new xowl.lang.owl2.DataPropertyAssertion();
            axiom.setDataProperty(pIRI);
            axiom.setIndividual(current);
            axiom.setValueLiteral(lit);
            changeset.add(new xowl.interpreter.Change(onto, axiom, true));
        } else {
            xowl.lang.owl2.IRI obj = repo.getIRI(value);
            xowl.lang.owl2.ObjectPropertyAssertion axiom = new xowl.lang.owl2.ObjectPropertyAssertion();
            axiom.setObjectProperty(pIRI);
            axiom.setIndividual(current);
            axiom.setValueIndividual(obj);
            changeset.add(new xowl.interpreter.Change(onto, axiom, true));
        }
        counter++;
    }

    @Override public void addProperty(int property, int valueClass, String valueId) {
        addProperty(property, valueId, false);
    }
    
    private xowl.lang.owl2.Literal getLiteral(Object v) {
        xowl.lang.owl2.Literal lit = new xowl.lang.owl2.Literal();
        xowl.lang.runtime.Literal temp = (xowl.lang.runtime.Literal)repo.getLiteral(v);
        lit.setLexicalValue(temp.getLexicalValue());
        lit.setMemberOf(temp.getMemberOf().getInterpretationOf().getHasIRI());
        return lit;
    }
}
